#include "svglineitem.h"
SvgLineItem::SvgLineItem(QPointF start, QPointF end, int Width, QColor color, QBrush brush_input):
    CShape(start, end, Width, color, brush_input)
{
}
SvgLineItem::SvgLineItem(qreal start_x,qreal start_y, qreal end_x, qreal end_y,int Width, QColor color, QBrush brush_input):
    CShape(start_x, start_y, end_x, end_y, Width, color, brush_input)
{
}
void SvgLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) {
    Q_UNUSED(widget);
    // 由初始设定的起点和终点连线绘制直线
    painter->setPen(this->pen());
    painter->drawLine(points[S1], points[E1]);
    if(option->state & QStyle::State_Selected) PaintBoundingandControlPoints(painter,boundingRect().adjusted(POINT_SIZE.width() / 2, POINT_SIZE.height() / 2, -POINT_SIZE.width() / 2, -POINT_SIZE.height() / 2) );
}

QPainterPath SvgLineItem::shape() const{
    // 绘制直线路径
    QPainterPath path;
    path.moveTo(points[S1]);
    path.lineTo(points[E1]);
    // 填充直线路径
    QPainterPathStroker stroker;
    stroker.setWidth(pen().widthF()+10);
    QPainterPath strokerPath = stroker.createStroke(path);
    // 将控制点加入直线路径
    for (int i = 0; i < 8; i++) {
        strokerPath.addRect(PointsforDraw[i]);
    }
    return strokerPath;
}

QRectF SvgLineItem::boundingRect() const {
    return shape().boundingRect();
}



void SvgLineItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
    WitchPointsisSelected = -1;
    isRotating = false;
    if (event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier) {
        isRotating = true;
        setSelected(true);
        BeginScenePos = event->scenePos();
        BeginTransform = transform();
    }
    else {
        setCursor(Qt::CursorShape::SizeAllCursor);
        for (int i = 0; i < 8 ; i++) {
            if (PointsforDraw[i].contains(event->pos()))
            {
                WitchPointsisSelected = i;
                setSelected(true);
                break;
            }
        }
        if (WitchPointsisSelected!=-1) {
            MoveControlPoint(event->pos());
            prepareGeometryChange();
            update();
        }
        else {
            setCursor(Qt::CursorShape::SizeAllCursor);
            QGraphicsItem::mousePressEvent(event);
        }
    }
}
void SvgLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
     if (WitchPointsisSelected != -1)
     {
        setSelected(true);
        MoveControlPoint(event->pos());
        prepareGeometryChange();
        update();
     }
     else     if (isRotating&&(event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)) {
        SupportRotate(event->scenePos(),mapToScene(boundingRect().center()));
     }
     else QGraphicsItem::mouseMoveEvent(event);
}
void SvgLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
     setCursor(Qt::CursorShape::ArrowCursor);
     if(WitchPointsisSelected != -1) {
        WitchPointsisSelected = -1;
        update();
     }
     QGraphicsItem::mouseReleaseEvent(event);
}
